package com.blog.blog.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.blog.blog.dao.mapper.CategoryMapper;
import com.blog.blog.dao.mapper.InterestTribeMapper;
import com.blog.blog.dao.pojo.Category;
import com.blog.blog.dao.pojo.CategoryUser;
import com.blog.blog.dao.pojo.SysUser;
import com.blog.blog.service.InterestTribeService;
import com.blog.blog.utils.UserThreadLocal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

@Service
public class InterestTribeServiceImpl implements InterestTribeService {

    @Autowired
    private InterestTribeMapper interestTribeMapper;
    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public List<JSONObject> findAllPersonalInterestTribe() {
        SysUser sysUser = UserThreadLocal.get();
        LambdaQueryWrapper<CategoryUser> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(CategoryUser::getUserId,sysUser.getId());
        List<CategoryUser> categoryUsers = interestTribeMapper.selectList(lambdaQueryWrapper);
        ArrayList<JSONObject> list = new ArrayList<>();
        if (!categoryUsers.isEmpty()) {
            for (CategoryUser categoryUser : categoryUsers) {
                JSONObject jsonObject = new JSONObject(new LinkedHashMap<>());
                jsonObject.put("interestTribeId", categoryUser.getCategoryId());
                jsonObject.put("interestTribeName", categoryMapper.selectById(categoryUser.getCategoryId()).getCategoryName());
                list.add(jsonObject);
            }
        }
        return list;
    }
}
